package com.cg.service.impl;

import com.cg.common.TypeCommon;
import com.cg.dao.DictionaryMapper;
import com.cg.dao.OrderMapper;
import com.cg.dao.PatientMapper;
import com.cg.dto.*;
import com.cg.entity.Dictionary;
import com.cg.entity.Order;
import com.cg.service.OrderService;
import com.cg.service.PayService;
import com.cg.vo.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Order)表服务实现类
 *
 * @author chengong
 * @since 2024-01-14 17:01:04
 */
@Service("orderService")
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderMapper orderMapper;

    @Resource
    private PatientMapper patientMapper;

    @Resource
    private DictionaryMapper dictionaryMapper;

    @Resource(name = "payAlibabaServiceImpl")
    private PayService payAlibabaService;

    @Resource(name = "payWxServiceImpl")
    private PayService payWxService;

    /**
     * 预约信息列表
     */
    @Override
    public PageInfo<OrderListVo> queryOrderList(OrderPageDto orderPageDto) {
        PageHelper.startPage(orderPageDto.getPageNum(),orderPageDto.getPageSize());
        List<OrderListVo> orderListVos = orderMapper.queryOrderList(orderPageDto);
        PageInfo<OrderListVo> pageInfo = new PageInfo<>(orderListVos);
        return pageInfo;
    }


    /**
     * 获取患者信息及其预约信息
     */
    @Override
    public Result<PatientOrderVo> queryPatientOrder(OrderPageDto orderPageDto) {
        PatientVo patientVo = patientMapper.queryPatientVoByCardNum(orderPageDto.getCardNum());
        PageInfo<OrderListVo> pageInfo = this.queryOrderList(orderPageDto);
        Map<String,Object> map = new HashMap<>();
        map.put("pageInfo",pageInfo);
        PatientOrderVo patientOrderVo = new PatientOrderVo();
        patientOrderVo.setPatientVo(patientVo);
        patientOrderVo.setOrderPatientList(map);
        return ResultUtils.returnDataSuccess(patientOrderVo);
    }

    /**
     * 取号前查询订单支付方式
     */
    @Override
    public PayOrderVo getPayOrderByOrderNum(String orderNum) {
        //查询订单金额和订单编号
        OrderNumAndPriceVo orderNumAndPriceVo = orderMapper.queryOrderNumAndPrice(orderNum);
        List<Dictionary> dictionaryList = dictionaryMapper.queryPayType(TypeCommon.TYPE_CODE);
        PayOrderVo payOrderVo = new PayOrderVo();
        payOrderVo.setOrderNumAndPriceVo(orderNumAndPriceVo);
        payOrderVo.setDictionaryList(dictionaryList);
        return payOrderVo;
    }

    /**
     * 去支付
     */
    @Override
    public Result<String> toPay(PayDto payDto) {

        //判断，预约状态
        //判断就诊时间
        Integer count = orderMapper.checkOrder(payDto.getOrderNum());
        if(count == 0){
            return ResultUtils.returnResult(ResultEnum.ORDER_CHECK_ERROR);
        }

        //通过预约编码查询预约信息
        OrderNumAndPriceVo orderNumAndPriceVo = orderMapper.queryOrderNumAndPrice(payDto.getOrderNum());
        Order order = new Order();
        order.setOrderNum(orderNumAndPriceVo.getOrderNum());
        order.setPrice(orderNumAndPriceVo.getPrice());

        if(payDto.getPayType().equals(TypeCommon.PAY_TYPE_AL)){
            //支付宝支付
            String toPay = payAlibabaService.toPay(order);
            return ResultUtils.returnDataSuccess(toPay);
        }
        //去支付 TODO
        return null;
    }
}
